using UnityEngine;

namespace Framework.Utils.SingletonTemplate
{
    public class SingletonMonoBase<T> : MonoBehaviour where T : MonoBehaviour
    {
        private static T _instance;

        public static T Instance
        {
            get
            {
                if (_instance != null)
                    return _instance;
                var temp = Object.FindObjectsOfType(typeof(T));
                if (temp?.Length > 0)
                    foreach (var o in temp)
                        Object.Destroy(o);
                var obj = new GameObject($"Mgr::{typeof(T).Name}");
                var ins = obj.AddComponent<T>();
                _instance = ins;
                Object.DontDestroyOnLoad(obj);
                return _instance;
            }
        }
    }
}